package com.sunseagear.wind.modules.email.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.email.entity.EmailTemplate;
import com.sunseagear.wind.modules.email.entity.query.EmailTemplateQuery;
import com.sunseagear.wind.modules.email.service.IEmailTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.email.controller
 * @title: 邮件模板控制器
 * @description: 邮件模板控制器 * @date: 2018-09-12 10:59:18
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("/email/template")
@Log(title = "邮件模板")
@RequiredArgsConstructor
public class EmailTemplateController extends BaseBeanController {

    private final IEmailTemplateService emailTemplateService;

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @GetMapping(value = "list")
    @Log(title = "查询列表", logType = LogType.SELECT)
    @SaCheckPermission("email:template:list")
    public RPage<EmailTemplate> list(EmailTemplateQuery query) {
        //加入条件
        QueryWrapper<EmailTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(EmailTemplate::getCreateDate)
                .like(ObjectUtils.isNotEmpty(query.getName()),EmailTemplate::getName, query.getName())
                .like(ObjectUtils.isNotEmpty(query.getCode()),EmailTemplate::getCode, query.getCode());
        // 预处理
        Page<EmailTemplate> pageBean = emailTemplateService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("email:template:add")
    public R<Void> add(@Validated @RequestBody EmailTemplate entity) {
        String templateCode = StringUtils.randomString(10);
        entity.setCode(templateCode);
        emailTemplateService.save(entity);
        return R.success("添加成功");
    }


    @PutMapping("update")
    @SaCheckPermission("email:template:update")
    @Log(logType = LogType.UPDATE)
    public R<Void> update(@Validated @RequestBody EmailTemplate entity) {
        emailTemplateService.saveOrUpdate(entity);
        return R.success("更新成功");
    }

    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("email:template:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        emailTemplateService.removeBatchByIds(idList);
        return R.success("删除成功");
    }
}
